import { Component, OnInit, Output, Input, EventEmitter } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { _HttpClient } from '@delon/theme';

@Component({
    selector: 'app-operamodal',
    templateUrl: './operaModal.component.html',
    styleUrls: ['./manufactorSkill.component.less']
})
export class OperaModalComponent implements OnInit {
    Visible = false;
    operaData: any = {};
    objData: any = {};
    delData: any = { 'count': 0, 'id': [] };

    @Input() apiUrl: any;
    @Input() operaType: any;
    @Input() responseData: any;
    @Input() popTitle: any;
    @Output() public closeModal = new EventEmitter();
    @Output() public reloadData = new EventEmitter();

    constructor(
        private msg: NzMessageService,
        private http: _HttpClient,
    ) { }

    ngOnInit() {
        switch (this.operaType) {
            case 'add':
                this.getApi(() => {
                    // to do
                });
                break;
            case 'edit':
                let editStatus = false;
                let editCount = 0;
                for (const item of this.responseData) {
                    if (item.checked) {
                        editCount += 1;
                        this.objData = item;
                        editStatus = true;
                    }
                }
                if (editStatus && editCount === 1) {
                    this.getApi(() => {
                        // 回填数据
                        this.operaData.id = this.objData.id;
                        this.operaData.rymc = this.objData.person;
                        this.operaData.ly = this.objData.position;
                        this.operaData.dwmc = this.objData.factoryName;
                        this.operaData.rydh = this.objData.phone;
                        this.operaData.rysj = this.objData.cellphone;
                        this.operaData.bz = this.objData.remark;
                    });
                } else {
                    this.Cancel();
                    this.msg.error('请选择一条数据进行操作');
                }
                break;
            case 'del':
                let delStatus = false;
                for (const item of this.responseData) {
                    if (item.checked) {
                        this.objData = item;
                        this.delData.count += 1;
                        this.delData.id.push(item.id)
                        delStatus = true;
                    }
                }
                if (delStatus) {
                    this.getApi(() => {
                        // to do
                    });
                } else {
                    this.Cancel();
                    this.msg.error('请选择数据删除');
                }
                break;
        }
    }
    getApi(callback) {
        setTimeout(() => {
            this.Visible = true;
            callback();
        }, 200);
    }
    Ok() {
        let param: any = {};
        switch (this.operaType) {
            case 'add':
            case 'edit':
                if (!this.operaData.rymc || !this.operaData.ly || !this.operaData.dwmc || !this.operaData.rysj) {
                    this.msg.error('必填项不能为空', { nzDuration: 1000 });
                    return;
                }
                param = {
                    'person': this.operaData.rymc,
                    'position': this.operaData.ly,
                    'factoryName': this.operaData.dwmc,
                    'phone': this.operaData.rydh,
                    'cellphone': this.operaData.rysj,
                    'remark': this.operaData.bz
                }
                if (this.operaType === 'edit') {
                    param.id = this.operaData.id;
                }
                break;
            case 'del':
                param = this.delData.id;
                break;
        }
        this.http.post(this.apiUrl, param).subscribe((res: any) => {
            if (!res.code) {
                this.Visible = false;
                setTimeout(() => {
                    this.closeModal.emit();
                    this.reloadData.emit();
                }, 200);
            } else {
                this.msg.error('操作失败');
            }
        });
    }

    Cancel() {
        this.Visible = false;
        setTimeout(() => {
            this.closeModal.emit();
        }, 200);
    }
}